<?php
// SQL语句封装

class Sql {
    protected $dsn = 'mysql:dbname=mall;host=localhost;port=3306';
    protected $username = "jing";
    protected $password = "123456";

    // 数据库连接
    public function open()
    {
        try {
            $conn = new PDO($this->dsn, $this->username, $this->password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }

        return $conn;
    }

    // 增
    public function create($table, $columns = array(), $values = array())
    {
        $conn = $this->open();
        // 对应列和值
        for ($i = 0; $i < sizeof($columns); $i++)
        {
            $columns2 .= $columns[$i];
            $values2 .= "'".$values[$i]."'";

            if ($i < sizeof($columns) - 1) {
                $columns2 .= ",";
                $values2 .= ",";
            }
        }
        // 执行
        $sql = "insert into ".$table." (".$columns2.") values (".$values2.")"; // sql语句
        $result = $conn->query($sql);
        return $result->rowCount() == 1?1:0;
    }

    // 删
    public function delete($table, $columns = array(), $values = array())
    {
        $conn = $this->open();
        // 对应列和值
        for ($i = 0; $i < sizeof($columns); $i++)
        {
            $column .= $columns[$i]."='".$values[$i]."'";
            if (sizeof($columns) != 1 and $i < sizeof($columns) -1) {
                $column .= " and ";
            }
        }
        // 执行
        $sql = "delete from ".$table." where ".$column;
        $result = $conn->query($sql);
        return $result->rowCount() == 1?1:0;
    }

    // 改
    public function updata($table, $columns = array(), $values = array(), $keys = array(), $value = array())
    {
        $conn = $this->open();
        // 查询条件
        for ($i = 0; $i < sizeof($columns); $i++)
        {
            $column .= $columns[$i]."='".$values[$i]."'";
            if (sizeof($columns) != 1 and $i < sizeof($columns) -1) {
                $column .= " and ";
            }
        }
        // 修改值
        for ($i = 0; $i < sizeof($keys); $i++)
        {
            $key .= $keys[$i]."='".$value[$i]."'";
            if (sizeof($keys) != 1 and $i < sizeof($keys) -1) {
                $key .= " and ";
            }
        }
        // 执行
        $sql = "update ".$table." set ".$key." where ".$column;
        $result = $conn->query($sql);
        return $result->rowCount() == 1?1:0;
    }

    // 查 -- 检测是否存在
    public function query($table, $columns = array(), $values = array())
    {
        $conn = $this->open();
        // 对应列和值
        for ($i = 0; $i < sizeof($columns); $i++)
        {
            $column .= $columns[$i]."='".$values[$i]."'";
            if (sizeof($columns) != 1 and $i < sizeof($columns) -1) {
                $column .= " and ";
            }
        }
        // 执行
        $sql = "select * from ".$table." where ".$column;
        $result = $conn->query($sql);
        return $result->rowCount() == 1?true:false;
    }

    // 获取 -- 1行数据
    public function getdata($table, $columns = array(), $values = array())
    {
        $conn = $this->open();
        // 对应列和值
        for ($i = 0; $i < sizeof($columns); $i++)
        {
            $column .= $columns[$i]."='".$values[$i]."'";
            if (sizeof($columns) != 1 and $i < sizeof($columns) -1) {
                $column .= " and ";
            }
        }
        // 执行
        $sql = "select * from ".$table." where ".$column;
        $result = $conn->query($sql);
        // 获取到数组结果
        if ($result->rowCount() == 1) {
            foreach($result->FetchAll() as $key => $result) {
                // 这样会获取到key和index，就是数据重复
                $arr = $result;
            }
        }
        return $arr?$arr:0;
    }

    public function type()
    {
        echo "正在使用sql封装函数库";
    }
    
}


// $sql = new Sql;
// // $sql->connection();


// // 增
// $a = array('user','password');
// $ab = array('值13','2');
// $cc = $sql->create('test',$a,$ab);

// echo $cc;

// // 删除
// $a = array('user');
// $ab = array('值2');


// $cc =  $sql->delete('test',$a,$ab);
// echo $cc;


// 改
// $a = array('user','password');
// $a1 = array('值13','2');
// $b = array('email');
// $b1 = array('domycat2');
// echo $sql->updata('test',$a,$a1,$b,$b1);

// 查
// echo $sql->query('test',$a,$ab);

 
// 获取数据
// print_r($sql->getdata('test',$a,$ab));

// $sql = new Sql;